//
//  CTFinanceTransformInListVC.m
//  digitalCurrency
//
//  Created by apple on 2019/6/14.
//  Copyright © 2019 XinHuoKeJi. All rights reserved.
//

#import "CTFinanceTransformInListVC.h"
#import "CTQGCell.h"
#import "HomeNetManager.h"

@interface CTFinanceTransformInListVC ()
@property (nonatomic, strong) NSMutableArray *qglists;
@property (nonatomic, assign) NSInteger page;
@property (nonatomic, strong) LYEmptyView *emptyView;
@end

@implementation CTFinanceTransformInListVC

- (instancetype)init
{
    self = [super init];
    if (self) {
        _qglists = [NSMutableArray array];
        _page = 1;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.viewTitle.text = LocalizationKey(@"Roll-in list");
    
    [self.view addSubview:self.tableView];
    self.tableView.rowHeight = 62;
    self.tableView.separatorColor = [UIColor clearColor];
    [self.tableView registerClass:[CTQGCell class] forCellReuseIdentifier:@"qgcell"];
    
    [self headRefreshWithScrollerView:self.tableView];
    [self footRefreshWithScrollerView:self.tableView];
    
    [self doFinancingsList];
}

- (void)viewDidLayoutSubviews {
    [super viewDidLayoutSubviews];
    self.tableView.frame = self.view.bounds;
}

#pragma mark-下拉刷新数据
- (void)refreshHeaderAction {
    _page = 1;
    [self doFinancingsList];
}

- (void)refreshFooterAction {
    _page++;
    [self doFinancingsList];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _qglists.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    CTQGCell *cell = [CTQGCell createTableView:tableView];
    
    NSDictionary *dic = _qglists[indexPath.row];
    cell.createTime.text = [ToolUtil timeIntervalToTimeString:dic[@"time"] WithDateFormat:@"YY-MM-dd HH:mm:ss"];
    cell.coinNumber.text = [NSString stringWithFormat:@"%@",[ToolUtil judgeStringForDecimalPlaces:[dic[@"amount"] stringValue] point:4]];
    cell.coinName.text = dic[@"unit"];
    
    cell.bgView.frame = CGRectMake(0, 0, kWindowW, 62);
    cell.bgView.layer.cornerRadius = 0.01;
    cell.bgView.layer.shadowColor = [UIColor whiteColor].CGColor;
    
    if (indexPath.row%2!=0) {
        cell.bgView.backgroundColor = [[UIColor colorWithHEX:mainBlackColor] colorWithAlphaComponent:0.5];
    }
    else {
        cell.bgView.backgroundColor = [UIColor whiteColor];
    }
    
    return cell;
}

- (void)doFinancingsList {
    NSDictionary *dic = @{@"pageNum":@(_page),@"pageSize":@"20",@"coinId":self.unit};
    [HomeNetManager doFinancingsListWithInfo:dic CompleteHandle:^(id resPonseObj, int code) {
        if (code) {
            NSInteger codeInt = [resPonseObj[@"code"] integerValue];
            if (codeInt == 0) {
                NSArray *data = resPonseObj[@"data"];
                
                if (_page == 1) {
                    [self.qglists removeAllObjects];
                }
                else {
                    if (data.count == 0) {
                        self.tableView.mj_footer.state = MJRefreshStateNoMoreData;
                    }
                }
                
                for (NSDictionary *sub in data) {
                    [self.qglists addObject:sub];
                }
                self.tableView.ly_emptyView = self.emptyView;
                [self.tableView reloadData];
            }
            else {
                [self.view makeToast:resPonseObj[MESSAGE] duration:1.5 position:CSToastPositionCenter];
            }
        }
        else {
            [self.view makeToast:LocalizationKey(@"noNetworkStatus") duration:1.5 position:CSToastPositionCenter];
        }
    }];
}
- (LYEmptyView *)emptyView {
    if (!_emptyView) {
        _emptyView = [LYEmptyView emptyViewWithImageStr:@"emptyData" titleStr:[[ChangeLanguage bundle] localizedStringForKey:@"NoData" value:nil table:@"English"]];
        _emptyView.titleLabTextColor = tThreeColor;
        _emptyView.titleLabFont = [UIFont systemFontOfSize:19];
    }
    return _emptyView;
}
@end
